package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.List;

public class SearchItemsServlet extends HttpServlet {
    private static final String VIEW_ITEM = "/WEB-INF/jsp/catalog/SearchItems.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    //用于处理从搜索页面跳转至商品展示界面的请求
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(666);
        String productId = request.getParameter("productId");


        CatalogService service = new CatalogService();

        //将页面渲染需要的值存入session中
        Product product = service.getProduct(productId);
        List<Item> itemList = service.getItemListByProduct(productId);


        HttpSession session = request.getSession();
        session.setAttribute("itemList", itemList);
        session.setAttribute("product", product);

        request.getRequestDispatcher(VIEW_ITEM).forward(request, response);
    }
}
